﻿using MediatR;
using Traceability.App.Api.Application.Command.Detail.Cultivation;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Cultivation;

namespace Traceability.App.Api.Application.Handler.Detail.Cultivation
{
    public class GetCultivationCommpanyHandler : IRequestHandler<GetCultivationCommpanyCommand, ApiResult<CultivationCommpanyModel>>
    {
        IBaseRepository<CultivationCommpanyModel> culcomRepo;

        public GetCultivationCommpanyHandler(IBaseRepository<CultivationCommpanyModel> culcomRepo)
        {
            this.culcomRepo = culcomRepo;
        }

        public Task<ApiResult<CultivationCommpanyModel>> Handle(GetCultivationCommpanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<CultivationCommpanyModel> res = new ApiResult<CultivationCommpanyModel>();
            var ssss = culcomRepo.GetAll().Where(c => c.Batch == request.Batch).OrderByDescending(c => c.Id).FirstOrDefault();
            if (ssss == null)
            {
                res.Msg = "错误";
                res.Code = ApiEnum.Fail;
                return Task.FromResult(res);
            }
            res.Msg = "成功";
            res.Code = ApiEnum.Success;
            res.Datas = ssss;
            return Task.FromResult(res);
        }
    }
}
